<?php
/**
 * 站点配置模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class ConfigAction extends CommonAction {
	/**
	 * 基本信息设置 
	 * @see CommonAction::index()
	 */
	public function index() {
		if($this->isPost()){
			$this->saveConfig();
		}else{
			$this->display();
		}
	}
	
	/**
	 * 验证码设置 
	 */
	public function code(){
		if($this->isPost()){
			$this->saveConfig();
		}else{
			$this->display();
		}
	}
	
	/**
	 * 客服扣扣设置
	 */
	public function qq(){
		if($this->isPost()){
			$this->saveConfig();
		}else{
			$this->display();
		}
	}
	
	/**
	 * 链接设置
	 */
	public function link(){
		if($this->isPost()){
			$this->saveConfig();
		}else{
			$this->display();
		}
	}
	
	/**
	 * 服务项目设置
	 */
	public function service(){
		if($this->isPost()){
			$config=M('Config');
			foreach($_POST as $key=>$val){
				$data    = array(
					'value'=>($key=='EMAIL_AUTH_PWD')? myEncode($val):$val,
					'add_time'=>time()
				);
				$where =  "name='".$key."'";
				$config->where($where)->save($data);
			}
			//查询出所有配置并生成文件缓存
			$list = $config->getField('name,value');
			//缓存文件名称
			$savefile = DATA_PATH . '~config.php';
			//所有配置参数统一为大写
			$content = "<?php\nreturn " . var_export(array_change_key_case($list, CASE_UPPER), true) . ";\n?>";
			if (!file_put_contents($savefile, $content)) {
				$this->error('服务项目修改失败！');
			}
			$this->success('服务项目修改成功！');
		}else{
			$this->display();
		}
	}
	
	/**
	 * 缩略图设置
	 */
	public function thumb(){
		if($this->isPost()){
			$this->saveConfig();
		}else{
			$this->display();
		}
	}
	
	/**
	 * 保存
	 */
	public function saveConfig(){
		$config=M('Config');
		foreach($_POST as $key=>$val){
			$data    = array(
				'value'=>($key=='EMAIL_AUTH_PWD')? myEncode($val):$val,
				'add_time'=>time()
			);
			$where =  "name='".$key."'";
			$config->where($where)->save($data);
		}
		//查询出所有配置并生成文件缓存
		$list = $config->getField('name,value');
		//缓存文件名称
		$savefile = DATA_PATH . '~config.php';
		//所有配置参数统一为大写
		$content = "<?php\nreturn " . var_export(array_change_key_case($list, CASE_UPPER), true) . ";\n?>";
		if (!file_put_contents($savefile, $content)) {
			$this->error('配置缓存失败！');
		}
		$this->success('配置修改成功！');
	}
	
}
?>